home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / ftp / archie132.lha / archie-1.3.2 / src / stcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  2.0 KB  |  108 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  */
  7.  
  8. #include <copyright.h>
  9. #include <stdio.h>
  10. #include <pmachine.h>
  11.  
  12. #include <stdlib.h>
  13.  
  14.  
  15. #ifdef NEED_STRING_H
  16. # include <string.h>
  17. #else
  18. # include <strings.h>
  19. #endif
  20.  
  21. #if defined(MSDOS)
  22. # include <stdlib.h>
  23. #endif
  24.  
  25. char    *stcopyr();
  26.  
  27. int    string_count = 0;
  28. int    string_max = 0;
  29.  
  30. /*
  31.  * stcopy - allocate space for and copy a string
  32.  *
  33.  *     STCOPY takes a string as an argument, allocates space for
  34.  *     a copy of the string, copies the string to the allocated space,
  35.  *     and returns a pointer to the copy.
  36.  */
  37.  
  38. char *
  39. stcopy(st)
  40.     char    *st;
  41.     {
  42.       if (!st) return(NULL);
  43.       if (string_max < ++string_count) string_max = string_count;
  44.  
  45.       return strcpy((char *)malloc(strlen(st) + 1), st);
  46.     }
  47.  
  48. /*
  49.  * stcopyr - copy a string allocating space if necessary
  50.  *
  51.  *     STCOPYR takes a string, S, as an argument, and a pointer to a second
  52.  *     string, R, which is to be replaced by S.  If R is long enough to
  53.  *     hold S, S is copied.  Otherwise, new space is allocated, and R is
  54.  *     freed.  S is then copied to the newly allocated space.  If S is
  55.  *     NULL, then R is freed and NULL is returned.
  56.  *
  57.  *     In any event, STCOPYR returns a pointer to the new copy of S,
  58.  *     or a NULL pointer.
  59.  */
  60. char *
  61. stcopyr(s,r)
  62.     char    *s;
  63.     char    *r;
  64.     {
  65.     int    sl;
  66.  
  67.     if(!s && r) {
  68.         free(r);
  69.         string_count--;
  70.         return(NULL);
  71.     }
  72.     else if (!s) return(NULL);
  73.  
  74.     sl = strlen(s) + 1;
  75.  
  76.     if(r) {
  77.         if ((strlen(r) + 1) < sl) {
  78.         free(r);
  79.         r = (char *) malloc(sl);
  80.         }
  81.     }
  82.     else {
  83.         r = (char *) malloc(sl);
  84.         string_count++;
  85.         if(string_max < string_count) string_max = string_count;
  86.     }
  87.         
  88.     return strcpy(r,s);
  89.     }
  90.  
  91. /*
  92.  * stfree - free space allocated by stcopy or stalloc
  93.  *
  94.  *     STFREE takes a string that was returned by stcopy or stalloc 
  95.  *     and frees the space that was allocated for the string.
  96.  */
  97. void
  98. stfree(st)
  99.     char *st;
  100.     {
  101.     if(st) {
  102.         free(st);
  103.         string_count--;
  104.     }
  105.     }
  106.  
  107.  
  108.